import 'package:flutter/material.dart';
import 'package:photo_pipeline/photo_pipeline.dart';

import '../constants/colors.dart';
import 'base64_cached_image.dart';

/// 上传任务列表中使用的缩略图组件。
///
/// 复用了首页的二级缓存策略（内存/磁盘 -> base64 -> 相机拉取），
/// 并在加载、失败时提供统一的占位展示。
class UploadTaskThumbnail extends StatelessWidget {
  const UploadTaskThumbnail({
    super.key,
    required this.fileInfo,
    this.size = 56,
  });

  final FileInfo? fileInfo;
  final double size;

  @override
  Widget build(BuildContext context) {
    Widget child;

    if (fileInfo != null) {
      child = Base64CachedImage(
        key: ValueKey<int?>(fileInfo!.id),
        fileInfo: fileInfo!,
        fit: BoxFit.cover,
        filterQuality: FilterQuality.low,
        placeholder: const _ThumbnailPlaceholder(),
        errorWidget: const _ThumbnailError(),
      );
    } else {
      child = const _ThumbnailPlaceholder();
    }

    return Container(
      width: size,
      height: size,
      decoration: BoxDecoration(
        color: AppColors.cardBackground,
        borderRadius: BorderRadius.circular(12),
      ),
      clipBehavior: Clip.antiAlias,
      child: child,
    );
  }
}

class _ThumbnailPlaceholder extends StatelessWidget {
  const _ThumbnailPlaceholder();

  @override
  Widget build(BuildContext context) {
    return const Center(
      child: Icon(
        Icons.cloud_upload,
        color: AppColors.textSecondary,
      ),
    );
  }
}

class _ThumbnailError extends StatelessWidget {
  const _ThumbnailError();

  @override
  Widget build(BuildContext context) {
    return const Center(
      child: Icon(
        Icons.broken_image_outlined,
        color: AppColors.textSecondary,
      ),
    );
  }
}


